package com.bawei.lib_common.utils

/**
 *作者：lxy
 *功能:
 *时间：2025/5/13
 */
object StringUtils {

    fun join(iterable: Iterable<*>?, separator: String?): String? {
        if (iterable == null) {
            return null
        }
        return join(iterable.iterator(), separator)
    }

    fun join(iterator: Iterator<*>?, separator: String?): String? {
        // handle null, zero and one elements before building a buffer

        if (iterator == null) {
            return null
        }
        if (!iterator.hasNext()) {
            return ""
        }
        val first = iterator.next()
        if (!iterator.hasNext()) {
            @Suppress("deprecation") // ObjectUtils.toString(Object) has been deprecated in 3.2
            val result = objectToString(first)
            return result
        }

        // two or more elements
        val buf = StringBuilder(256) // Java default is 16, probably too small
        if (first != null) {
            buf.append(first)
        }

        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator)
            }
            val obj = iterator.next()
            if (obj != null) {
                buf.append(obj)
            }
        }
        return buf.toString()
    }

    fun objectToString(obj: Any?): String {
        return obj?.toString() ?: ""
    }

    fun isBlank(cs: CharSequence?): Boolean {
        var strLen: Int = 0
        if (cs == null || (cs.length.also { strLen = it }) == 0) {
            return true
        }
        for (i in 0 until strLen) {
            if (!Character.isWhitespace(cs[i])) {
                return false
            }
        }
        return true
    }
}